<?php

require 'classes/Config.php';

abstract class BaseController {
    
    /**
     * Parameters given to this request in the url
     */
    protected $urlParams;
    
    /**
     * Configuration object
     * @var Config 
     */
    protected $config;
    
    /**
     * The action to run in the object
     */
    private $_action;
    
    /**
     * Response to serve to the client from the method
     */
    protected $result;
    
    /**
     * Instantiate and load configuration
     */
    public function __construct($action, $urlParams) {
        $this->_action = $action;
        $this->urlParams = $urlParams;
        $this->loadConfig('config/config.php');
        
        // set up database connection
        $this->setupDatabase();
    }
    
    private function setupDatabase() {
        
        if($_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
            $host     = $this->config->get('db_hostname');
            $dbname   = $this->config->get('db_database');
            $user     = $this->config->get('db_username');
            $password = $this->config->get('db_password');
        } else {
            $host     = $this->config->get('local_db_hostname');
            $dbname   = $this->config->get('local_db_database');
            $user     = $this->config->get('local_db_username');
            $password = $this->config->get('local_db_password');
        }
        
        $connString = sprintf('mysql:host=%s;dbname=%s', $host, $dbname);
        ORM::configure($connString);
        ORM::configure('username', $user);
        ORM::configure('password', $password);
        
    }
    
    /**
     * Checks whether or not the current request was invoked through 
     * a POST request
     */
    public function isPostRequest() {
        return $_SERVER['REQUEST_METHOD'] == 'POST';
    }
    
    /**
     * Sets the return to indicate one or more errors has occurred
     * @param $fieldErrors field => description of errors to report
     */
    public function reportErrors($fieldErrors) {
        $response = array();
        $response['result'] = 'error';
        $response['errors'] = $fieldErrors;
        
        $this->result = $response;
    }
    
    /**
     * Sets the return to indicate that the operation was a success
     */
    public function reportSuccess() {
        $response = array();
        $response['result'] = 'success';
        
        $this->result = $response;
    }
    
    /**
     * Runs the set action and prints the result (or errors, if any)
     */
    public function run() {
        $action = $this->_action;
        // call method with url parameters as arguments
        $result = call_user_func_array(array($this, $action), $this->urlParams);
        
        if(empty($this->result)) {
            $this->result = $result;
        }
        
        $this->printResult($this->result);
        
    }
    
    /**
     * Loads a configuration file
     */
    private function loadConfig($file) {
        $this->config = new Config();
        $configArray = require $file;
        $this->config->setAll($configArray);
    }
    
    /**
     * Prints the action result as json
     */
    private function printResult($result) {
        header('Content-Type: application/json');
        echo json_encode($result);
    }
    
    /**
     * Sends a 404 header to the client
     */
    protected function http404() {
        header("HTTP/1.1 404 Not Found");
        die;
    }
    
}
